<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ClassifyController extends Controller
{
    /**
     * 商品分类表
     */
    public function catalog_list(Request $request,$tag_id)
    {
        $res = $request->method();
        if($res == 'GET')
        {
            //分配商品标签
            $tag = DB::table('tag')->get();
            //分配商品分类
            $classify = DB::table('classify')->where('tag_id','=',$tag_id)->get();
            //分配商品
            $goods = DB::table('goods')
                ->where(['goods.status'=>1])
                ->where('tag_id','=',$tag_id)
                ->leftJoin('img','goods.goods_id','=','img.goods_id')
                ->paginate(3);
            //分配session信息到主页模板
            $user_info= session('sign_user_info');
            return view('Home/Classify/catalog_list')->with('classify',$classify)->with('tag',$tag)->with('goods',$goods)->with('user_info',$user_info);
        }

        if($res == 'POST')
        {
            $c_id = $request->input();
            //根据商品分类id 获取该分类下的商品列表
            $data = DB::table('goods')
                ->where(['goods.status'=>1])
                ->where('c_id','=',$c_id['c_id'])
                ->where('tag_id','=',$tag_id)
                ->leftJoin('img','goods.goods_id','=','img.goods_id')
                ->get();
            return $data;
        }

    }
}
